package com.cloudm.technician.ui.base

import com.cloudm.technician.data.contants.Constant
import com.cloudm.technician.data.model.LoadMoreInfo
import com.cloudm.technician.data.model.Response
import com.cloudm.technician.extensions.logger
import com.google.gson.JsonParseException
import io.reactivex.Observable
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import org.json.JSONException
import retrofit2.HttpException
import java.net.ConnectException
import java.net.SocketException
import java.net.SocketTimeoutException

/**
 * 作者: wesley
 * 时间: 2017/11/20
 * 描述: 处理取消请求的presenter
 */
abstract class RxPresenter : BasePresenter {

    private val compositeDisposable = CompositeDisposable()

    protected fun <T> request(observable: Observable<Response<T?>>, view: BaseView,
                              onSuccess: (T?, LoadMoreInfo?) -> Unit, onFailure: (String) -> Unit) {

        observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).filter {
            view.isActive
        }.subscribe(object : Observer<Response<T?>> {
            override fun onNext(t: Response<T?>) {
                when (t.code) {
                    800 -> onSuccess(t.result, t.page)
                    300 -> view.openLoginActivityAgain()
                    -300 -> view.openMachineListActivityAgain(
                            t.message ?: Constant.STATUS_CODE_ERROR)
                    else -> {
                        t.message?.let {
                            onFailure(it)
                        }
                    }
                }
            }

            override fun onComplete() {

            }

            override fun onSubscribe(d: Disposable) {
                compositeDisposable.add(d)
            }

            override fun onError(e: Throwable) {
                if (!view.isActive) {
                    logger(" onError view is not active")
                    return
                }
                logger(" 异常信息是 = ${e.message}")
                when (e) {
                    is HttpException -> {
                        when (e.code()) {
                            404 -> onFailure(Constant.CANNOT_CONTACT_TO_SERVER)
                            504 -> onFailure(Constant.NETWORK_ERROR)
                            else -> onFailure(Constant.UNKNOWN_ERROR)
                        }
                    }
                    is ConnectException -> onFailure(Constant.CANNOT_CONTACT_TO_SERVER)
                    is SocketException -> onFailure(Constant.NETWORK_ERROR)
                    is SocketTimeoutException -> onFailure(Constant.CONNECT_TIME_OUT)
                    is JsonParseException, is JSONException -> onFailure(
                            Constant.DATA_PARSE_EXCEPTION)
                    else -> onFailure(Constant.UNKNOWN_ERROR)
                }
            }
        })
    }

    override fun cancelRequest() {
        compositeDisposable.clear()
    }
}